package top.maof.book.spider.develop.contents.gushiwen;

import lombok.*;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.spider.develop.Split;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.util.Date;

/**
 * 古诗文网
 * 古文作者不易收集 该字段一律用mao代替
 */
@Data
@TargetUrl("https://so.gushiwen.cn/guwen/book_\\w+.aspx")
@HelpUrl({"https://so.gushiwen.cn/guwen/default.aspx\\?p=\\d+"})
public class Gushiwen implements AfterExtractor, Split {
    @ExtractBy("//div[@id=sonsyuanwen]/div[@class=cont]/h1/span/b/text()")
    private String name;

    @ExtractBy("//div[@id=sonsyuanwen]/div[@class=cont]/div[@class=divimg]/img/@src")
    private String img;

    @ExtractBy("//div[@id=sonsyuanwen]/div[@class=cont]/p/text()")
    private String synopsis;

    private String url;

    private String content;

    private Integer webId = 10;

    public static final String CHARSET = "utf-8";

    public static final String PROTOCOL = "https";

    public static final String URL = "https://www.gushiwen.cn";

    @Override
    public Book getBook() {
        Book book = new Book();
        book.setState("完本");
        book.setAuthor("mao");
        book.setImg(this.img);
        book.setName(this.name);
        book.setInsertDate(new Date());
        book.setClassify("古籍");
        book.setSynopsis(this.synopsis);
        return book;
    }

    @Override
    public Contents getContents() {
        Contents contents = new Contents();
        contents.setUrl(this.url);
        contents.setContent(this.content);
        contents.setWebId(this.webId);
        return contents;
    }

    @Override
    public void afterProcess(Page page) {

        // img字段做处理
        if (!this.img.startsWith("http")) {
            if (this.img.startsWith("//")) {
                this.img = PROTOCOL + ":" + this.img;
            } else if (this.img.startsWith("/"))
                this.img = URL + this.img;
            else {
                this.img = page.getUrl().toString().substring(0, page.getUrl().toString().lastIndexOf("/") + 1) + this.img;
            }
        }

        this.content = page.getHtml().xpath("//div[@class=sons]").toString();

        //获取该页面url
        this.url = page.getRequest().getUrl();

    }


}
